package com.blackline.auto.Report;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.interactions.Actions;

import com.blackline.auto.common.CommonMethods;
import com.blackline.declareUI.UI;
//import com.newb.AutoDriver.AutoDriver;
import com.blackline.parameters.*;

/**
 * @author XBO
 * @version Feb, 2013
 * <p>The smoke test to Reports module.
 */
public class Reports_scheduletest {
	private WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();
	private static Logger logger=Logger.getLogger(Reports_smoketest.class);
	UI UI=new UI();
	Parameters parameters;
	@Before
	public void setUp() throws Exception {
		FirefoxProfile profile=new FirefoxProfile();
		profile.setEnableNativeEvents(true);
		parameters=new Parameters("parameters.xml");
		driver=new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		UI.declareAllUI(driver);
	}

	@Test
	public void Reportsscheduletest() throws Exception {
		int sleepTime = 5000;
		//driver.get(pa.urlDev2);
		Date now=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
		Calendar cal=Calendar.getInstance();
		cal.setTime(now);
		cal.add(Calendar.DAY_OF_MONTH, 1);
		String startDate=sdf.format(cal.getTime());
		logger.info(startDate);

		SimpleDateFormat sdf2=new SimpleDateFormat("yyyy");
		String years=sdf2.format(cal.getTime());
		String endDate="12/31/"+years;
		logger.info(endDate);
		
//		WebDriverBackedSelenium aaa=new WebDriverBackedSelenium(driver,"");
		
		logger.info("You've selected site: https://dev.blacklineondemand.com/xbosoft4");
		UI.LogonPage.Login(driver, "https://dev.blacklineondemand.com/xbosoft4", parameters.getLogonID("admin"), parameters.getPassword("admin"));
		Thread.sleep(3000);
		  UI.NavigationMenu.REPORTS.click();
		  UI.Reports.Enhanced_Reporting_link.click();
		  Thread.sleep(20000);
		  UI.Reporting.AddIcon_BlackLineReports.click();
		  Thread.sleep(20000);
		  UI.Reporting.AddIcon_Account.click();
		  Thread.sleep(20000);
		  UI.Reporting.Information_nodeInLeftTree.click();
		  Thread.sleep(sleepTime + 3);
		  UI.Reporting.Name_text_field.sendKeys("User Access");
		  UI.Reporting.Refresh_button.click();
		  Thread.sleep(sleepTime + 5000);
		  UI.Reporting.Schedule_linkOfSearchedResult.click();
		  Thread.sleep(sleepTime);
		  UI.SchedulePrompts.Schedule_Name_Text.sendKeys("user access schedule");
		  //UI.SchedulePrompts.
		  UI.SchedulePrompts.Schedule_Start_Date_Text.clear();
		  UI.SchedulePrompts.Schedule_Start_Date_Text.sendKeys(startDate);
		  UI.SchedulePrompts.Schedule_Start_Date_Text.click();
		  UI.SchedulePrompts.Schedule_End_Date_Text.clear();
		  UI.SchedulePrompts.Schedule_End_Date_Text.sendKeys(endDate);
		 
		  UI.SchedulePrompts.Schedule_End_Date_Text.click();
		  Thread.sleep(3000);
	      UI.SchedulePrompts.Schedule_Report_button.click();
	      Thread.sleep(3000);
	      String runRes=UI.Alert_inReporting.Message_Text.getText();
			if(runRes.contains("has been created"))
			  {
				  logger.info("[Pass!] "+runRes);
				  UI.Alert_inReporting.OK_button.click();
			  }
			  else{
				  logger.error("[Fail!] "+runRes);
				  verificationErrors.append("[Fail!] "+runRes);
				  fail("Schedule failed: "+runRes);
			  }
		  Thread.sleep(8000);
		  if(new CommonMethods().doesUIExist(UI.SchedulePrompts.Cancel_button)){
			  logger.info("Cancel button is exsit");
			  if(UI.SchedulePrompts.Cancel_button.isDisplayed()){
				  logger.info("Cancel button is displayed");
			  UI.SchedulePrompts.Cancel_button.click();
			  Thread.sleep(3000);
			  }
		  }
		  UI.Reporting.My_scheduled_Report_nodeInLeftTree.click();
		  Thread.sleep(10000);
		  UI.Reporting.Name_text_field.sendKeys("user access schedule");
		  //UI.Reporting.Name_text_field.sendKeys(Keys.ENTER);
		  UI.Reporting.Refresh_button.click();
		  Thread.sleep(sleepTime);
		  String schedulename = UI.Reporting.Schedule_Name_SearchedResult.getText();
		  logger.info(schedulename);	  
		  //Wait for the status to change from Running to Ready
	 
		  logger.info("after clicking..................");
		  Thread.sleep(sleepTime);
		 
		  if ("user access schedule".equals(schedulename))
		  {
			  logger.info("The report  that was Scheduled exists in My scheduled report folder " );
			  logger.info("The schedule name is : "+ schedulename);
		  }
		  else
		  {
			  verificationErrors.append("The report schedule  " + schedulename + "is not created successfully");
		  }
		  
		//  UI.SessionMenu.LogOut.click();
	}

	@After
	public void tearDown() throws Exception {
		CommonMethods.takeScreenShot(this.getClass(), driver);
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}
}
